
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新闻编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="/static/js/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
          media="all">
    <link rel="stylesheet" type="text/css" href="/static/css/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="/static/css/personal.css" media="all">
</head>
<body>
<section class="layui-larry-box">
    <div class="larry-personal">
        <header class="larry-personal-tit">
            <a href="/admin/news?type=<%=type%>" class="layui-btn layui-btn-primary">返回</a>
            <span id="title-span">Loading...</span>
        </header><!-- /header -->
        <div class="larry-personal-body clearfix">
            <form class="layui-form" action="" method="post">
                <div class="layui-form-item">
                    <label class="layui-form-label">文章类型</label>
                    <div class="layui-input-block">
                        <input type="text" autocomplete="off" class="layui-input layui-disabled" id="title_cate" value="<%=type%>" disabled="disabled">
                        <input type="hidden" name="cate" value="<%=type%>">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">文章标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title"  autocomplete="off" class="layui-input" value="<%=news.title%>" required  lay-verify="title">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">是否置顶</label>
                    <div class="layui-input-block">
                        <input type="radio" name="top" value="1" title="置顶" <% if(news.top && news.top==1){ %>checked<% } %> ><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><span>置顶</span></div>
                        <input type="radio" name="top" value="0" title="不置顶" <% if(!news.top || (news.top && news.top==0)){ %>checked<% } %>><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><span>不置顶</span></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">封面</label>
                    <div class="layui-input-block">
                        <input type="file" name="file" class="layui-upload-file">
                        <input type="hidden" name="cover" value="<%=news.cover%>">
                        <button type="button" class="layui-btn layui-btn-small layui-btn-radius layui-btn-danger" onclick="showImg()" id="show-btn" <% if(!news.cover){ %>style="display: none;"<% } %>>查看图片</button>
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">文章内容</label>
                    <div class="layui-input-block">
                        <textarea name="content" placeholder="请输入文章内容" class="layui-textarea" id="content-textarea"><%=news.content%></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                        <a href="/admin/news?type=<%=type%>" class="layui-btn layui-btn-primary">返回</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</section>
<script type="text/javascript" src="/static/js/layui/layui.js"></script>
<script type="text/javascript">
    var CFG = {hyxx:'行业信息',gsxw:'公司新闻',rdgz:'热点关注',rsgg:'人事公告'};
    layui.config({
        base: '/static/js/layui/ext/'
    }).use(['form','upload','layedit','layer','common'],function(){
        var form = layui.form(),
            layer = layui.layer,
            $=layui.jquery,
            layedit = layui.layedit,
            common = layui.common;
        window.$ = $,
            window.layer = layer;
        var type = '<%=type%>',
            str = CFG[type],
            _id='<%=news._id%>';

        var editLayer = layedit.set({
            uploadImage: {
                url: '/upload'
            }
        }).build('content-textarea');

        $("#title-span").text(str),
            $("#title_cate").val(str);
        layui.upload({
            url: '/upload' ,//上传接口
            success: function(res){
                if(res.code==0){
                    $("#show-btn").show();
                    $(":hidden[name='cover']").val(res.data.src);
                }else{
                    layer.msg("上传失败");
                }
            }
        });

        form.verify({
            title: function(value){
                if(common.isEmpty(value)){
                    return '标题不能为空';
                }else if(value.length<6 || value.length>50){
                    return '标题必须在6~50个字符之间';
                }
            }
        });

        form.on('submit(*)', function (data) {
            var load = layer.load(1, {
                shade: [0.1,'#fff'],
                time: 10*1000
            });
            var params = data.field;
            delete params.file;

            params.content = layedit.getContent(editLayer);
            if(_id){
                params._id = _id;
            }
            console.log(params);
            if(common.isEmpty(params.content)){
                layer.closeAll();
                common.layerAlertE('内容不能为空');
                return false;
            }
            common.ajax('/admin/news/save',params,function(resp) {
                common.layerAlertS("保存成功");
                location.href='/admin/news?type=<%=type%>';

            });
            return false;
        });

    });
    var showImg = function(){
        var img = $(":hidden[name='cover']").val();
        if(!img) return;
        var json = {
            "title": "封面查看", //相册标题
            "id": 999, //相册id
            "data": [
                {
                    "alt": "封面查看",
                    "pid": 666,
                    "src": img,
                    "thumb": img
                }
            ]
        }
        layer.photos({photos:json})
    }
</script>
</body>
</html>